package oracle.apps.financials.collection.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.adapter.ListOfValueAdapter;
import oracle.apps.financials.collection.adapter.ListOfValueAdapter.ViewHolder;
import oracle.apps.financials.collection.po.Customer;
import oracle.apps.financials.collection.util.MockDataFactory;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

public class BusinsessUnitWindowListener extends BaseOnClickListener {

	private ListOfValueAdapter adapter;
	private ListView listView;
	private View layout;

	private List<HashMap<String, Object>> valueList = new ArrayList<HashMap<String, Object>>();

	String name[] = new String[] { "Vision Operations", "Vision Operations (USA)", "Vision Foods", "Vision XY", "Vision ABC", "Vision Operations ABC" };

	public BusinsessUnitWindowListener(Context context, Customer customer, Handler mHandler) {
		super(context, R.layout.add_bu_parameter, customer, mHandler);
		adapter = new ListOfValueAdapter(context, valueList, R.layout.list_view_item, new String[] { "item_tv", "item_cb" }, new int[] {
				R.id.item_textView, R.id.item_checkBox });

		for (int i = 0; i < name.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("item_tv", name[i]);
			map.put("item_cb", true);
			valueList.add(map);
			adapter.selectedMap.put(i, true);
		}

	}

	@Override
	public void onClick(View parent) {
		showPopupwindow(parent);
	}

	public void showPopupwindow(View parent) {
		if (popupWindow == null) {
			initPopupWindow();
		}
		popupWindow.showAsDropDown(parent, -80, 1);
	}

	public void initPopupWindow() {

		popupWindow = new PopupWindow(popupWindowView, 250, 250);

		listView = (ListView) popupWindowView.findViewById(R.id.bu_list);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ViewHolder vHollder = (ViewHolder) view.getTag();
				vHollder.checkbox.toggle();
				adapter.selectedMap.put(position, vHollder.checkbox.isChecked());
				getMockCustomerData();
				BusinsessUnitWindowListener.this.notifyHandler(customer);
			}

		});

		listView.setAdapter(adapter);

		processButtonEvent();

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	public void getMockCustomerData() {
		customer.getMetrics()
				.getPercentCurrent()
				.setyValueList(
						MockDataFactory.getCustomerList().get(Math.abs(MockDataFactory.random.nextInt() % 10)).getMetrics().getPercentCurrent()
								.getyValueList());
	}

	public void processButtonEvent() {

		Button cancelButton = (Button) popupWindowView.findViewById(R.id.cancel);

		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				BusinsessUnitWindowListener.this.dismissPopupWindow();
			}

		});

		Button checkAllButton = (Button) popupWindowView.findViewById(R.id.check_all);

		checkAllButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				for (int i = 0; i < valueList.size(); i++) {
					adapter.selectedMap.put(i, true);
				}
				adapter.notifyDataSetChanged();
				getMockCustomerData();
				BusinsessUnitWindowListener.this.notifyHandler(customer);
			}

		});

		Button uncheckAllButton = (Button) popupWindowView.findViewById(R.id.uncheck_all);

		uncheckAllButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				for (int i = 0; i < valueList.size(); i++) {
					adapter.selectedMap.put(i, false);
				}
				adapter.notifyDataSetChanged();
				getMockCustomerData();
				BusinsessUnitWindowListener.this.notifyHandler(customer);
			}

		});

	}

	public ListOfValueAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(ListOfValueAdapter adapter) {
		this.adapter = adapter;
	}

}
